package org.example;

import ai.djl.ModelException;
import ai.djl.translate.TranslateException;
import com.alibaba.fastjson2.JSON;
import org.apache.commons.lang.StringUtils;
import org.example.config.ApplicationProperties;
import org.example.config.Configuration;
import org.example.constants.CommonConstants;
import org.example.model.Browser;
import org.example.model.Device;
import org.example.script.KsJsExecutor;
import org.example.ui.UIExecutor;
import org.example.utils.*;
import org.openqa.selenium.WebDriver;

import java.io.IOException;

/**
 * Hello world!
 */
public class App {

    public static void main(String[] args) throws IOException, ModelException, TranslateException {
        UIExecutor uiExecutor = new UIExecutor();
        uiExecutor.initUi();
        // start();
        // test();
        // test2();
//         GlobalUtils.logi("get args[0]:" + args[0]);
//         start(args[0]);
    }

    private static void start(String configPath) {
        // 读取配置
        Configuration configuration = new Configuration();
        ApplicationProperties applicationProperties = null;
        try {
            applicationProperties = configuration.readProperties(configPath);
        } catch (Exception e) {

        }


        // 初始化
        // OCR初始化
        OcrUtils.initOcr(applicationProperties.getOcrType(), applicationProperties.getOcrPath(), applicationProperties.getOcrDataPath());

        String userDataPath = applicationProperties.getUserDataPath();
        CommonConstants.offsetX = Integer.parseInt(applicationProperties.getOffsetX());
        CommonConstants.offsetY = Integer.parseInt(applicationProperties.getOffsetY());
        CommonConstants.saveImgPath = applicationProperties.getSaveImgPath();
        CommonConstants.saveLogPath = applicationProperties.getSaveLogPath();

        String executeStr = applicationProperties.getExecuteArr();

        WebDriver webDriver = GlobalUtils.startDriver(userDataPath);
        Browser browser = BrowserUtils.initBrowser(webDriver);
        GlobalUtils.logi("browser info");
        GlobalUtils.logi(JSON.toJSONString(browser));
        GlobalUtils.sleep(8000);

        Device device = DeviceUtils.initDeviceInfo(webDriver);
        GlobalUtils.logi("device info");
        GlobalUtils.logi(JSON.toJSONString(device));
        if (StringUtils.isNotEmpty(executeStr)) {
            String [] executeArray = executeStr.split(",");
            for (int i = 0; i < executeArray.length; i++) {
                String executeSwitch = executeArray[i];
                if ("ks_js_open".equals(executeSwitch)) {
                    KsJsExecutor ksJsExecutor = new KsJsExecutor(webDriver, device);
                    ksJsExecutor.execute();
                } else if ("hg_mfdj_open".equals(executeSwitch)) {

                }
            }
        }




        // OcrUtils.release();
        // System.out.println("脚本执行结束！");
    }
}
